/*
* @author : TF-BJ-C064
* @creation : 2014-6-20 上午11:11:11
* @description : 
*
*/

package com.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.base.BaseDao;
import com.bean.Test;
import com.util.GUtil;

public class TestDao extends BaseDao implements BaseDaoInterface {
	
	private static final Logger log = LoggerFactory.getLogger(Test.class);

	@Override
	public Object get(Integer id) throws Exception {
		log.debug("getting "+Test.class+" instance with id: " + id);
		try {
			Test  instance = (Test) getSession().get(Test.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List<Object> query(Object object) throws Exception {
		Test test = (Test) object;
		log.debug("query "+Test.class+" instances");
		try {
			String queryString = "from "+Test.class;
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("query failed", re);
			throw re;
		}
	}

	@Override
	public String update(Object object) throws Exception {
		session = getSession();
		Test test = (Test) object;
		if(session==null || test==null)
			return GUtil.FAILE;
		log.debug("updating "+Test.class+" instance");
		try {
			Timestamp tm = new Timestamp(System.currentTimeMillis());
			test.setLastUpdate(tm);
			session.update(test);
			log.debug(Test.class+" update successful");
		} catch (RuntimeException re) {
			log.error(Test.class+" update failed", re);
			throw re;
		}
		return GUtil.SUCCESS;
	}

	@Override
	public String delete(Integer id, Integer flag) throws Exception {
		session = getSession();
		Test test = (Test) this.get(id);
		if(session==null || test==null)
			return GUtil.FAILE;
		log.debug("deleting "+Test.class+" instance");
		try {
			if(flag==null){
				session.delete(test);
			}else{
				Timestamp tm = new Timestamp(System.currentTimeMillis());
				test.setLastUpdate(tm);
				session.update(test);
			}
			log.debug(Test.class+" delete successful");
		} catch (RuntimeException re) {
			log.error(Test.class+" delete failed", re);
			throw re;
		}
		return GUtil.SUCCESS;
	}

	@Override
	public List findByProperty(String propertyName, Object value) throws Exception {
		log.debug("finding "+Test.class+" instance with property: "+ propertyName + ", value: " + value);
		try {
			String queryString = "from "+Test.class+" as model where model."+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByExample(Object object) throws Exception {
		Test instance = (Test) object;
		log.debug("finding "+Test.class+" instance by example");
		try {
			List results = getSession().createCriteria(Test.class)
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	
}
